package clin.filesysclin;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.UUID;
public class SocCaozuo {
	Socket socket = null;
	OutputStream out = null;
	InputStream in = null;
	String uuid = null;
	private String ip = null;
	public SocCaozuo(){
		uuid = UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	public void connect() {
		try {
			socket = new Socket(ip,8999);
			out = socket.getOutputStream();
			in = socket.getInputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void addfile_loc(String path) throws IOException {
		File f = new File(path);
		FileInputStream fin = new FileInputStream(f);
		BufferedInputStream bin = new BufferedInputStream(fin);
		addfile(f.getName(), f.length(), bin);
	}
	
	public void getfile_loc(String name,String path) throws IOException {
		String ss = path;
		if((ss.substring(ss.length()-1)).equals("/")) {
			ss = path + name;
		}else {
			ss = path + "/" + name;
		}
		FileOutputStream fin = new FileOutputStream(ss);
		BufferedOutputStream bou = new BufferedOutputStream(fin);
		getfile(name,bou);
	}
	
	public void addfile(String name,long size,BufferedInputStream inp) throws IOException {
		connect();
		String str = "fangfa:addfiletext:name:"+name+"size:"+String.valueOf(size)+"uuid:"+uuid;
		byte[] by = str.getBytes();
		byte[] bin = new byte[18];
		out.write(by);
		out.flush();
		int leng = 0;
		while(leng < 18) {
			leng = in.read(bin,leng,18-leng);
		}
		byte[] buf = new byte[4096];
		int c = 0;
		while((c = inp.read(buf)) != -1) {
			out.write(buf, 0, c);
			out.flush();
			c = 0;
		}
		inp.close();
		System.out.println(getRec(in)[1]);
		closeall();
		
	}
	
	public void getfile(String name,BufferedOutputStream outl) throws IOException {
		connect();
		String str = "fangfa:getfiletext:"+name+"uuid:"+uuid;
		out.write(str.getBytes());
		out.flush();
		byte[] buf = new byte[4096];
		int c = 0;
		while((c = in.read(buf)) != -1){
			outl.write(buf, 0, c);
			outl.flush();
			c = 0;
		}
		outl.close();
		closeall();
		System.out.println("ok");
	}
	
	public void remfile(String name) throws IOException {
		connect();
		String str = "fangfa:remfiletext:"+name+"uuid:"+uuid;
		out.write(str.getBytes());
		out.flush();
		System.out.println(getRec(in)[1]);
		closeall();
	}
	
	public void createdir(String name) throws IOException {
		connect();
		String str = "fangfa:creatdirtext:"+name+"uuid:"+uuid;
		out.write(str.getBytes());
		out.flush();
		System.out.println(getRec(in)[1]);
		closeall();
	}
	
	public void qiehuandir(String name) throws IOException {
		connect();
		String str = "fangfa:qiehuandirtext:"+name+"uuid:"+uuid;
		out.write(str.getBytes());
		out.flush();
		System.out.println(getRec(in)[1]);
		closeall();
	}
	
	public long getusedsize() throws IOException {
		connect();
		String str = "fangfa:getusedsizetext:getusedsize"+"uuid:"+uuid;
		out.write(str.getBytes());
		out.flush();
		String[] bsin =  getRec(in);
		long size = Long.valueOf(bsin[1].trim());
		closeall();
		return size;
	}
	
	public long getallsize() throws IOException {
		connect();
		String str = "fangfa:getallsizetext:getallsize"+"uuid:"+uuid;
		out.write(str.getBytes());
		out.flush();
		String[] bsin =  getRec(in);
		long size = Long.valueOf(bsin[1].trim());
		closeall();
		return size;
	}
	
	public String getnowdir() throws IOException {
		connect();
		String str = "fangfa:getnowdirtext:getnowdir"+"uuid:"+uuid;
		out.write(str.getBytes());
		out.flush();
		String[] bsin =  getRec(in);
		closeall();
		return bsin[1];
	}
	
	public void nowdir(boolean m) throws IOException {
		String str = getnowdir();
		str = str.substring(2, str.length()-3);
		String sq[] = str.split("\\},\\{");
		for(int i=0;i<sq.length;++i) {
			String sqq[] = sq[i].split(",");
			System.out.print(sqq[0].substring(8,sqq[0].length()-1)+"    ");
			long si = Long.valueOf(sqq[1].substring(8, sqq[1].length()-1));
			if(m) {
				if (si < 1024) {
					System.out.print(si + "bit");
				} else if (si < 1024 * 1024 && si >= 1024) {
					System.out.print(si / 1024 + "Kb");
				} else if (si < 1024 * 1024 * 1024 && si >= 1024 * 1024) {
					System.out.print(si / (1024 * 1024) + "Mb");
				} else if (si >= 1024 * 1024 * 1024) {
					System.out.print(si / (1024 * 1024 * 1024) + "Gb");
				}
			}else {
				System.out.print(si + "bit");
			}
			System.out.print("    ");
			if(sqq[2].equals("\"leixing\":\"1\"")) {
				System.out.println("普通文件");
			}else {
				System.out.println("目錄");
			}
		}
		
		
	}
	
	public double getusedbaifenbi() throws IOException {
		connect();
		String str = "fangfa:getusedbaifenbitext:getusedbaifenbi"+"uuid:"+uuid;
		out.write(str.getBytes());
		String[] bsin =  getRec(in);
		closeall();
		return Double.valueOf(bsin[1].trim());
	}
	
	public void exit() throws IOException {
		connect();
		String str = "fangfa:exittext:exit"+"uuid:"+uuid;
		out.write(str.getBytes());
		out.flush();
		System.out.println(getRec(in)[1]);
		closeall();
	}
	
	public void closeall() throws IOException{
		socket.close();
		out.close();
		in.close();
	}
	
	public String[] getRec(InputStream inp) throws IOException {
        ArrayList<Byte> al = new ArrayList<>();
        byte bt = 0;
        while((bt = (byte)in.read()) != -1){
            al.add(bt);
        }
		//byte[] wby = in.readAllBytes();
        byte[] wby = new byte[al.size()];
        for(int i = 0;i<al.size();++i){
            wby[i] = al.get(i);
        }
		char[] cby = new char[wby.length];
		for(int i=0;i<wby.length;++i) {
			cby[i] = (char)wby[i];
		}
		String sin = String.valueOf(cby);
		String[] bsin = sin.split("text:");
		return bsin;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}
}
